﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;

namespace TrainManage.Controllers
{
    public class PartsController : Controller
    {
        //
        // GET: /Parts/
        PartsBll pas = new PartsBll();
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 添加配件视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Parts()
        {
            return View();
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="images"></param>
        /// <returns></returns>
        public ActionResult AddFile(HttpPostedFileBase images)
        {
            
           if(images!=null)
           {
               //将文件保存道服务端
               images.SaveAs(Server.MapPath("/phone/" +images.FileName));
               Session["Path"] = "/phone/" + images.FileName;
               Session["name"] = Request["names"];
               Session["filename"] = images.FileName;
               return Content("<script>alert('上传成功');location.href='/Parts/Parts'</script>");
           }
           else
           {
               return Content("<script>alert('上传失败');location.href='/Parts/Parts'</script>");
           }
        }

        /// <summary>
        /// 反填车厢节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string BackName(int id)
        {
            RailWayBll rwbll = new RailWayBll();
           var list= rwbll.ShowRw().Where(t => t.RailWayId == id).FirstOrDefault();
           Session["WagonId"] = list.WagonId;
           Session["RailWayId"] = list.RailWayId;
           Session["RailWayName"] = list.RailWayName;
           return list.RailWayName;
        }

        /// <summary>
        /// 添加配件信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool AddParts(int id)
        { 
           
            if(id==1)
            {
                Parts part= new Model.Parts();
                part.RailWayId=Convert.ToInt32(Session["RailWayId"]);
                part.PartsName=Session["name"].ToString();
                part.PartsPicture = Session["Path"].ToString();
               if(pas.AddParts(part)==true)
               {
                   return true;
               }
               else
               {
                   return false;
               }


            }else
                if(id==2)
                {
                    RailWayBll rwbll = new RailWayBll();
                    RailWay rw = new RailWay();
                    rw.WagonId = Convert.ToInt32(Session["WagonId"]);
                    rw.RailWayName = Session["name"].ToString();
                    rw.RailWayPicture = Session["filename"].ToString();
                    if (rwbll.AddRailWay(rw) == true)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
        }
        /// <summary>
        /// 父级节点
        /// </summary>
        /// <returns></returns>
        public JsonResult Partent()
        {
            WagonBll wgbll = new WagonBll();

            var Partent = wgbll.ShowWg().ToList();
            foreach (var tree in Partent)
            {
                PartentId(tree.WagonId);//调用子节点
            }
            return Json(Partent);
        }

        /// <summary>
        /// 子级节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult PartentId(int id)
        {

            RailWayBll rwbll = new RailWayBll();
            var Partent = rwbll.ShowRw().Where(m => m.WagonId == id).ToList();
            foreach (var tree in Partent)
            {
                PartentLastId(tree.RailWayId);//调用子节点
            }
            return Json(Partent);
        }

       /// <summary>
       /// 子子节点
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public JsonResult PartentLastId(int id)
        {
            PartsBll partsbll = new PartsBll();
            var Partent = partsbll.ShowParts().Where(m => m.RailWayId == id).ToList();
             return Json(Partent);
        }
    }
}
